package indi.kider.server.employee.api;

import indi.kider.common.model.employee.Worker;
import indi.kider.server.employee.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by sq on 2018/6/4.
 */
@RestController
@RequestMapping("worker")
public class WorkerController {
    @Autowired
    private WorkerService workerService;
    /**
     * @api {GET} /{id}
     * @apiGroup Worker
     * @apiDescription 根据id获取安装人员信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/Worker/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","remark":"2"}}
     * */
    @GetMapping("{id}")
    public CommonResponse<Worker> getWorkerById(@PathVariable Long id) {
        return CommonResponse.buildGetSuccessResponse(workerService.getWorkerById(id));
    }
    /**
     * @api {POST} /list
     * @apiGroup Worker
     * @apiDescription 根据字段查询获取安装人员列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/Worker/list
     * {"num":"0","size":"8","query":{"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","remark":"2"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","remark":"2"}],"total":1,"size":0}}
     * */
    @PostMapping("/list")
    public CommonResponse getWorkerPages(@RequestBody PageRequest<Worker> request) {
        return CommonResponse.buildGetSuccessResponse(workerService.getWorkerPages(request));
    }
    /**
     * @api {POST} /list/all
     * @apiGroup Worker
     * @apiDescription 根据字段查询获取安装人员列表（不分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/Worker/list/all
     * {"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","remark":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","remark":"2"}]}
     * */
    @PostMapping("/list/all")
    public CommonResponse getWorkers(@RequestBody Worker Worker) {
        return CommonResponse.buildGetSuccessResponse(workerService.getWorkers(Worker));
    }
    /**
     * @api {POST} /
     * @apiGroup Worker
     * @apiDescription 新增安装人员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/Worker
     * {"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","remark":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"id":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","remark":"2"}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody Worker Worker, HttpServletRequest request) {
        try {
            return CommonResponse.buildAddSuccessResponse(workerService.add(Worker));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {PUT} /
     * @apiGroup Worker
     * @apiDescription 修改安装人员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/Worker
     * {"id":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"23","remark":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改成功！","data":{"id":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"23","remark":"2"}}
     */
    @PutMapping("")
    public CommonResponse modify(@RequestBody Worker Worker) {
        try {
            return CommonResponse.buildUpdateSuccessResponse(workerService.modify(Worker));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {DELETE} /{id}
     * @apiGroup Worker
     * @apiDescription 删除安装人员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/Worker/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}}
     */
    @DeleteMapping("/{id}")
    public CommonResponse delById(@PathVariable Long id) {
        workerService.delById(id);
        return CommonResponse.buildDelSuccessResponse("");
    }
}
